package com.stable.service;

import com.alibaba.fastjson.JSON;
import com.stable.constant.RedisConstant;
import com.stable.es.dao.base.EsNewFinanceDao;
import com.stable.spider.tushare.TushareSpider;
import com.stable.utils.RedisUtil;
import com.stable.vo.bus.StockBaseInfo;
import com.stable.vo.spi.req.EsQueryPageReq;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Semaphore;

/**
 * 基本数据查询
 *
 * @author zhouwen
 */
@Service
@Log4j2
public class BasicFindService {
    @Autowired
    TushareSpider tushareSpider;
    @Autowired
    private EsNewFinanceDao esNewFinanceDao;
    @Autowired
    private RedisUtil redisUtil;
    private final Semaphore semapProBar = new Semaphore(1);


    // 查询
    public List<StockBaseInfo> queryBasicFind(String code,
                                              String name,
                                              String industry,
                                              String sshy,
                                              String conceptName,
                                              String gsld,
                                              EsQueryPageReq queryPage) {

        List<StockBaseInfo> infos = new ArrayList<>();
        Map<Object, Object> objectMap = redisUtil.hGetAll(RedisConstant.stock_basic);
        if (objectMap.size() == 0) {
            return infos;
        } else {
            objectMap.values().stream().forEach(info -> {
                StockBaseInfo stockBaseInfo = JSON.parseObject((String) info, StockBaseInfo.class);
                if (StringUtils.isNotBlank(code)) {
                    if (StringUtils.equals(stockBaseInfo.getCode(), code)) {
                        log.info("code| code:{},name:{},industry:{},sshy:{},conceptName:{},gsld:{}", stockBaseInfo.getCode(), stockBaseInfo.getName(), stockBaseInfo.getIndustry(), stockBaseInfo.getSshy(), stockBaseInfo.getConcept_name(), stockBaseInfo.getGsld());
                        infos.add(stockBaseInfo);
                    }
                    return;
                }
                if (StringUtils.isNotBlank(name)) {
                    if (StringUtils.isNotBlank(stockBaseInfo.getName()) && !stockBaseInfo.getName().contains(name)) {
                        log.info("code|name code:{},name:{},industry:{},sshy:{},conceptName:{},gsld:{}", stockBaseInfo.getCode(), stockBaseInfo.getName(), stockBaseInfo.getIndustry(), stockBaseInfo.getSshy(), stockBaseInfo.getConcept_name(), stockBaseInfo.getGsld());
                        infos.add(stockBaseInfo);
                    }
                    return;
                }

                if (StringUtils.isNotBlank(industry) && StringUtils.isNotBlank(sshy) && StringUtils.isNotBlank(conceptName) && StringUtils.isNotBlank(gsld)) {
                    if (StringUtils.isNotBlank(stockBaseInfo.getIndustry()) && StringUtils.isNotBlank(stockBaseInfo.getSshy()) && StringUtils.isNotBlank(stockBaseInfo.getConcept_name()) && StringUtils.isNotBlank(stockBaseInfo.getGsld())) {
                        if (stockBaseInfo.getIndustry().contains(industry) && stockBaseInfo.getSshy().contains(sshy) && stockBaseInfo.getConcept_name().contains(conceptName) && stockBaseInfo.getGsld().contains(gsld)) {
                            log.info("industry|sshy|conceptName|gsld code:{},name:{},industry:{},sshy:{},conceptName:{},gsld:{}", stockBaseInfo.getCode(), stockBaseInfo.getName(), stockBaseInfo.getIndustry(), stockBaseInfo.getSshy(), stockBaseInfo.getConcept_name(), stockBaseInfo.getGsld());
                            infos.add(stockBaseInfo);
                        }
                    }
                    return;
                }

                if (StringUtils.isNotBlank(industry) && StringUtils.isNotBlank(sshy) && StringUtils.isNotBlank(conceptName)) {
                    if (StringUtils.isNotBlank(stockBaseInfo.getIndustry()) && StringUtils.isNotBlank(stockBaseInfo.getSshy()) && StringUtils.isNotBlank(stockBaseInfo.getConcept_name())) {
                        if (stockBaseInfo.getIndustry().contains(industry) && stockBaseInfo.getSshy().contains(sshy) && stockBaseInfo.getConcept_name().contains(conceptName)) {
                            log.info("industry|sshy|conceptName code:{},name:{},industry:{},sshy:{},conceptName:{},gsld:{}", stockBaseInfo.getCode(), stockBaseInfo.getName(), stockBaseInfo.getIndustry(), stockBaseInfo.getSshy(), stockBaseInfo.getConcept_name(), stockBaseInfo.getGsld());
                            infos.add(stockBaseInfo);
                        }
                    }
                    return;
                }
                if (StringUtils.isNotBlank(sshy) && StringUtils.isNotBlank(conceptName) && StringUtils.isNotBlank(gsld)) {
                    if (StringUtils.isNotBlank(stockBaseInfo.getSshy()) && StringUtils.isNotBlank(stockBaseInfo.getConcept_name()) && StringUtils.isNotBlank(stockBaseInfo.getGsld())) {
                        if (stockBaseInfo.getSshy().contains(sshy) && stockBaseInfo.getConcept_name().contains(conceptName) && stockBaseInfo.getGsld().contains(gsld)) {
                            log.info("sshy|conceptName|gsld code:{},name:{},industry:{},sshy:{},conceptName:{},gsld:{}", stockBaseInfo.getCode(), stockBaseInfo.getName(), stockBaseInfo.getIndustry(), stockBaseInfo.getSshy(), stockBaseInfo.getConcept_name(), stockBaseInfo.getGsld());
                            infos.add(stockBaseInfo);
                        }
                    }
                    return;
                }
                if (StringUtils.isNotBlank(industry) && StringUtils.isNotBlank(conceptName) && StringUtils.isNotBlank(gsld)) {
                    if (StringUtils.isNotBlank(stockBaseInfo.getIndustry()) && StringUtils.isNotBlank(stockBaseInfo.getConcept_name()) && StringUtils.isNotBlank(stockBaseInfo.getGsld())) {
                        if (stockBaseInfo.getIndustry().contains(industry) && stockBaseInfo.getConcept_name().contains(conceptName) && stockBaseInfo.getGsld().contains(gsld)) {
                            log.info("industry|conceptName|gsld code:{},name:{},industry:{},sshy:{},conceptName:{},gsld:{}", stockBaseInfo.getCode(), stockBaseInfo.getName(), stockBaseInfo.getIndustry(), stockBaseInfo.getSshy(), stockBaseInfo.getConcept_name(), stockBaseInfo.getGsld());
                            infos.add(stockBaseInfo);
                        }
                    }
                    return;
                }
                if (StringUtils.isNotBlank(industry) && StringUtils.isNotBlank(sshy) && StringUtils.isNotBlank(gsld)) {
                    if (StringUtils.isNotBlank(stockBaseInfo.getIndustry()) && StringUtils.isNotBlank(stockBaseInfo.getSshy()) && StringUtils.isNotBlank(stockBaseInfo.getGsld())) {
                        if (stockBaseInfo.getIndustry().contains(industry) && stockBaseInfo.getSshy().contains(sshy) && stockBaseInfo.getGsld().contains(gsld)) {
                            log.info("industry|sshy|gsld code:{},name:{},industry:{},sshy:{},conceptName:{},gsld:{}", stockBaseInfo.getCode(), stockBaseInfo.getName(), stockBaseInfo.getIndustry(), stockBaseInfo.getSshy(), stockBaseInfo.getConcept_name(), stockBaseInfo.getGsld());
                            infos.add(stockBaseInfo);
                        }
                    }
                    return;
                }


                if (StringUtils.isNotBlank(industry) && StringUtils.isNotBlank(sshy)) {
                    if (StringUtils.isNotBlank(stockBaseInfo.getIndustry()) && StringUtils.isNotBlank(stockBaseInfo.getSshy())) {
                        if (stockBaseInfo.getIndustry().contains(industry) && stockBaseInfo.getSshy().contains(sshy)) {
                            log.info("industry|sshy code:{},name:{},industry:{},sshy:{},conceptName:{},gsld:{}", stockBaseInfo.getCode(), stockBaseInfo.getName(), stockBaseInfo.getIndustry(), stockBaseInfo.getSshy(), stockBaseInfo.getConcept_name(), stockBaseInfo.getGsld());
                            infos.add(stockBaseInfo);
                        }
                    }
                    return;
                }
                if (StringUtils.isNotBlank(industry) && StringUtils.isNotBlank(conceptName)) {
                    if (StringUtils.isNotBlank(stockBaseInfo.getIndustry()) && StringUtils.isNotBlank(stockBaseInfo.getConcept_name())) {
                        if (stockBaseInfo.getIndustry().contains(industry) && stockBaseInfo.getConcept_name().contains(conceptName)) {
                            log.info("industry|conceptName code:{},name:{},industry:{},sshy:{},conceptName:{},gsld:{}", stockBaseInfo.getCode(), stockBaseInfo.getName(), stockBaseInfo.getIndustry(), stockBaseInfo.getSshy(), stockBaseInfo.getConcept_name(), stockBaseInfo.getGsld());
                            infos.add(stockBaseInfo);
                        }
                    }
                    return;
                }
                if (StringUtils.isNotBlank(industry) && StringUtils.isNotBlank(gsld)) {
                    if (StringUtils.isNotBlank(stockBaseInfo.getIndustry()) && StringUtils.isNotBlank(stockBaseInfo.getGsld())) {
                        if (stockBaseInfo.getIndustry().contains(industry) && stockBaseInfo.getGsld().contains(gsld)) {
                            log.info("industry|gsld code:{},name:{},industry:{},sshy:{},conceptName:{},gsld:{}", stockBaseInfo.getCode(), stockBaseInfo.getName(), stockBaseInfo.getIndustry(), stockBaseInfo.getSshy(), stockBaseInfo.getConcept_name(), stockBaseInfo.getGsld());
                            infos.add(stockBaseInfo);
                        }
                    }
                    return;
                }
                if (StringUtils.isNotBlank(sshy) && StringUtils.isNotBlank(conceptName)) {
                    if (StringUtils.isNotBlank(stockBaseInfo.getSshy()) && StringUtils.isNotBlank(stockBaseInfo.getConcept_name())) {
                        if (stockBaseInfo.getSshy().contains(sshy) && stockBaseInfo.getConcept_name().contains(conceptName)) {
                            log.info("sshy|conceptName code:{},name:{},industry:{},sshy:{},conceptName:{},gsld:{}", stockBaseInfo.getCode(), stockBaseInfo.getName(), stockBaseInfo.getIndustry(), stockBaseInfo.getSshy(), stockBaseInfo.getConcept_name(), stockBaseInfo.getGsld());
                            infos.add(stockBaseInfo);
                        }
                    }
                    return;
                }
                if (StringUtils.isNotBlank(sshy) && StringUtils.isNotBlank(gsld)) {
                    if (StringUtils.isNotBlank(stockBaseInfo.getSshy()) && StringUtils.isNotBlank(stockBaseInfo.getGsld())) {
                        if (stockBaseInfo.getSshy().contains(sshy) && stockBaseInfo.getGsld().contains(gsld)) {
                            log.info("sshy|gsld code:{},name:{},industry:{},sshy:{},conceptName:{},gsld:{}", stockBaseInfo.getCode(), stockBaseInfo.getName(), stockBaseInfo.getIndustry(), stockBaseInfo.getSshy(), stockBaseInfo.getConcept_name(), stockBaseInfo.getGsld());
                            infos.add(stockBaseInfo);
                        }
                    }
                    return;
                }
                if (StringUtils.isNotBlank(conceptName) && StringUtils.isNotBlank(gsld)) {
                    if (StringUtils.isNotBlank(stockBaseInfo.getConcept_name()) && StringUtils.isNotBlank(stockBaseInfo.getGsld())) {
                        if (stockBaseInfo.getConcept_name().contains(conceptName) && stockBaseInfo.getGsld().contains(gsld)) {
                            log.info("conceptName|gsld code:{},name:{},industry:{},sshy:{},conceptName:{},gsld:{}", stockBaseInfo.getCode(), stockBaseInfo.getName(), stockBaseInfo.getIndustry(), stockBaseInfo.getSshy(), stockBaseInfo.getConcept_name(), stockBaseInfo.getGsld());
                            infos.add(stockBaseInfo);
                        }
                    }
                    return;
                }


                if (StringUtils.isNotBlank(industry)) {
                    if (StringUtils.isNotBlank(stockBaseInfo.getIndustry()) && stockBaseInfo.getIndustry().contains(industry)) {
                        log.info("industry code:{},name:{},industry:{},sshy:{},conceptName:{},gsld:{}", stockBaseInfo.getCode(), stockBaseInfo.getName(), stockBaseInfo.getIndustry(), stockBaseInfo.getSshy(), stockBaseInfo.getConcept_name(), stockBaseInfo.getGsld());
                        infos.add(stockBaseInfo);
                    }
                    return;
                }
                if (StringUtils.isNotBlank(sshy)) {
                    if (StringUtils.isNotBlank(stockBaseInfo.getSshy()) && stockBaseInfo.getSshy().contains(sshy)) {
                        log.info("sshy code:{},name:{},industry:{},sshy:{},conceptName:{},gsld:{}", stockBaseInfo.getCode(), stockBaseInfo.getName(), stockBaseInfo.getIndustry(), stockBaseInfo.getSshy(), stockBaseInfo.getConcept_name(), stockBaseInfo.getGsld());
                        infos.add(stockBaseInfo);
                    }
                    return;
                }
                if (StringUtils.isNotBlank(conceptName)) {
                    if (StringUtils.isNotBlank(stockBaseInfo.getConcept_name()) && stockBaseInfo.getConcept_name().contains(conceptName)) {
                        log.info("conceptName code:{},name:{},industry:{},sshy:{},conceptName:{},gsld:{}", stockBaseInfo.getCode(), stockBaseInfo.getName(), stockBaseInfo.getIndustry(), stockBaseInfo.getSshy(), stockBaseInfo.getConcept_name(), stockBaseInfo.getGsld());
                        infos.add(stockBaseInfo);
                    }
                    return;
                }
                if (StringUtils.isNotBlank(gsld)) {
                    if (StringUtils.isNotBlank(stockBaseInfo.getGsld()) && stockBaseInfo.getGsld().contains(gsld)) {
                        log.info("gsld code:{},name:{},industry:{},sshy:{},conceptName:{},gsld:{}", stockBaseInfo.getCode(), stockBaseInfo.getName(), stockBaseInfo.getIndustry(), stockBaseInfo.getSshy(), stockBaseInfo.getConcept_name(), stockBaseInfo.getGsld());
                        infos.add(stockBaseInfo);
                    }
                    return;
                }
            });
            return infos;
        }
    }


}
